package com.ruoyi.web.controller.bi.model;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rsbi.ext.engine.view.exception.ExtConfigException;
import com.ruoyi.bi.domain.model.DataSource;
import com.ruoyi.bi.service.model.DataSourceService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.demo.bo.TestDemoQueryBo;
import com.ruoyi.demo.domain.TestDemo;
import com.ruoyi.demo.vo.TestDemoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.bi.util.BaseController;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/model")
public class DataSourceController extends BaseController {

	@Autowired
	private DataSourceService dsService;

	/**
	 * 获取数据源
	 */
	@GetMapping("/listDataSource")
	public AjaxResult list(Map map){
		if (map.isEmpty()) {
			List<DataSource> list = dsService.list();
			return AjaxResult.success(list);
		} else {
			QueryWrapper<DataSource> wrapper=new QueryWrapper();
			wrapper.allEq(map);
			List<DataSource> list = dsService.list(wrapper);
			return AjaxResult.success(list);
		}
	}

	@GetMapping("/getDataSource")
	public AjaxResult get(@RequestParam String dsid){
		return AjaxResult.success(dsService.getById(dsid));
	}

	@DeleteMapping("/deleteDataSource/{dsid}")
	public AjaxResult delete(@PathVariable String dsid){
		return AjaxResult.success(dsService.removeById(dsid));
	}

	@PostMapping("/testDataSource")
	public Object test(@RequestBody DataSource ds) throws ExtConfigException {
		ds.setUse("jdbc");
		return dsService.testDataSource(ds);
	}

	@PostMapping("/testJndi")
	public Object testJndi(@RequestBody DataSource ds){
		ds.setUse("jndi");
		return dsService.testJNDI(ds);
	}

	@PostMapping("/saveDataSource")
	public AjaxResult save(@RequestBody DataSource ds){
		dsService.save(ds);
		return AjaxResult.success();
	}

	@PutMapping("/updateDataSource")
	public AjaxResult update(@RequestBody DataSource ds){
		dsService.updateById(ds);
		return AjaxResult.success();
	}
}
